<template>
  <div class="edit-birthday-container">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="handleCancel"
        @confirm="handleConfirm"
        />
  </div>
</template>

<script>
import dayjs from 'dayjs'
import { updateUserInfo } from 'network/user'
export default {
  name: 'EditBirthday',
  data () {
    return {
      minDate: new Date(1921, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(this.userInfo.birthday)
    }
  },
  props: {
    userInfo: {
      type: Object,
      required: true
    },
    isShowEditBirthday: {
      type: Boolean,
      required: true
    }
  },
  methods: {
    handleCancel () {
      this.$emit('update:isShowEditBirthday', false)
      this.currentDate = new Date(this.userInfo.birthday)
    },
    async handleConfirm (time) {
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0
      })
      const birthday = dayjs(time).format('YYYY-MM-DD')
      console.log(birthday)
      const res = await updateUserInfo({
        birthday
      })
      console.log(res)
      this.$emit('update:isShowEditBirthday', false)
      this.userInfo.birthday = birthday
      this.$toast.success('修改生日成功(＾－＾)V')
    }
  }
}
</script>

<style>

</style>
